<?php
/**
 * CreateTime: 2023/2/24 15:10
 */

namespace App\Http\Controllers\MeLawCase;

use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Func\TokenFuc;
use App\Http\Requests\ListBasicRequest;
use App\Http\Requests\MeLawCase\MeBoardAddRequest;
use App\Models\MeLawCase\MeLawCaseBoardModel;

class MeLawCaseBoardController
{
    public function addBoard(int $lawCaseId,MeBoardAddRequest $request){
        $staffInfo = TokenFuc::getCurrentStaffInfo();
        $data =  $request->all();
        $data['owner_id'] = $staffInfo['staff_id'];
        $data['law_case_id'] = $lawCaseId;
        $res = (new MeLawCaseBoardModel())->add($data);
        try {
            return ResponseFunc::returnJson(
                'ok',
                $res,
                '新增留言板成功',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }catch (\Exception $exception){
            return ResponseFunc::returnJson(
                'ok',
                $res,
                '新增留言板失败',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }
    }

    public function getBoardByLawCase(ListBasicRequest $request){
        try {
            $pFilters = $request->get('filters');
            $pageSize = $request->get('page_size');
            $filters = $pFilters ? json_decode($pFilters, true) : [];
            $query = (new MeLawCaseBoardModel());
            if($filters['law_case_id']){
                $query = $query->select([
                    "board_content",
                    "law_case_id",
                    "owner_id",
                    "created_time",
                    "owner_id as owner_str"
                ])->where('law_case_id',$filters['law_case_id']);
                $data = (new MeLawCaseBoardModel())->getListNoTime($query,$pageSize)->toArray() ?? [];
                return ResponseFunc::returnJson(
                    'ok',
                    $data,
                    '获取留言板成功',
                    ResponseStatusConsts::OK_200_RESPONSE_STATUS
                );
            }else{
                return ResponseFunc::returnJson(
                    'error',
                    null,
                    '获取留言板失败',
                    ResponseStatusConsts::OK_200_RESPONSE_STATUS
                );
            }
        }catch (\Exception $exception){
            return ResponseFunc::returnJson(
                'error',
                null,
                '获取留言板失败',
                ResponseStatusConsts::OK_200_RESPONSE_STATUS
            );
        }
    }


}